﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vowei.Core.Models;

namespace Vowei.Core
{
    public class ExcelRowSerializer
    {
        public static void Serialize(Dictionary<string, string> dic, IExcelRowConvertable to)
        {
            if (dic == null)
                throw new ArgumentNullException("dic");
            if (to == null)
                throw new ArgumentNullException("to");

            var type = to.GetType();
            var builder = new StringBuilder();
            foreach (var entry in dic)
            {
                if ((to is INamedTable) && entry.Key == "Title")
                {
                    var property = type.GetProperty(entry.Key);
                    property.SetValue(to, entry.Value, null);
                }
                else if ((to is ITable) && entry.Key == "ID")
                {
                    var property = type.GetProperty(entry.Key);
                    property.SetValue(to, Guid.Parse(entry.Value), null);
                }
                else
                {
                    builder.Append(entry.Key);
                    builder.Append(": ");
                    builder.Append(entry.Value);
                    builder.AppendLine();
                }
            }

            to.Description = builder.ToString();
            to.IsExcelExtended = true;
        }
    }
}
